package com.sailing.xjpb.orm.utils;

import com.sailing.xjpb.common.DataConverter;
import com.sailing.xjpb.common.exception.BusinessException;
import org.hibernate.transform.AliasedTupleSubsetResultTransformer;

/**
 * description 原始类型+String转换器
 *
 * @author 陈思佩
 * date 2024 年 05 月 29 日 19:41
 */
public class XtPrimitiveWrapperResultTransformer extends AliasedTupleSubsetResultTransformer {

    private static final long serialVersionUID = 2L;
    private final DataConverter dataConverter;
    private final Class<?> resultClass;

    public XtPrimitiveWrapperResultTransformer(Class<?> resultClass) {
        if (resultClass == null) {
            throw new IllegalArgumentException("resultClass cannot be null");
        }
        dataConverter = new DataConverter();
        this.resultClass = resultClass;
    }

    @Override
    public Object transformTuple(Object[] tuple, String[] aliases) {
        if (aliases.length != 1) {
            throw BusinessException.businessError(resultClass.getName() + "类型查询结果字段预期返回1列，实际返回"
                    + aliases.length + "列！");
        }
        return dataConverter.convert(tuple[0], resultClass);
    }

    @Override
    public boolean isTransformedValueATupleElement(String[] aliases, int tupleLength) {
        return false;
    }
}
